package com.pb.service.boot.config;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 *  决策管理器
 *  Authentication用户认证
 * @Author:liudebao
 * @Date: 2021/1/3
 */
@Component
public class CustomUrlDecisionManager implements AccessDecisionManager {
    @Override
    public void decide(Authentication authentication, Object object, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {
        for (ConfigAttribute configAttribute : configAttributes) {
            String needRole = configAttribute.getAttribute();
            if (needRole.indexOf("ILLEGAL_INTERFACE@") > -1) {
                throw new InsufficientAuthenticationException(needRole);
            } else if ("SESSION_EXPIRED".equals(needRole)) {
                throw new InsufficientAuthenticationException("SESSION_EXPIRED");
            } else {
                return;
            }
        }
    }

    @Override
    public boolean supports(ConfigAttribute attribute) {
        return true;
    }

    @Override
    public boolean supports(Class<?> clazz) {
        return true;
    }
}
